﻿using AutoMapper;
using B.S.Sterilizationcenter.Domain.zyy;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Writer.API.Appliaction.Commends.Zyy;
using Kdbndp.TypeHandlers;
using MediatR;
using SqlSugar;

namespace B.S.Sterilizationcenter.Writer.API.Appliaction.CommandHandlers.Zyy
{
    public class AddReceivingApplicationHandler : IRequestHandler<AddReceivingApplicationcommand, ApiResult<int>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public AddReceivingApplicationHandler(IMapper mapper,ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        public async Task<ApiResult<int>> Handle(AddReceivingApplicationcommand request, CancellationToken cancellationToken)
        {
           
            try
            {
                ApiResult<int> result = new ApiResult<int>();
                var data = mapper.Map<ReceivingApplication>(request);
                result.Data = await db.Insertable(data).ExecuteCommandAsync();
                if (result.Data > 0)
                {
                    result.Code = ApiEnums.添加成功;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Code = ApiEnums.添加失败;
                    result.Msg = "添加失败";
                }
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
